//pizza
package cn.framework.base.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.interceptor.ExceptionHolder;
import com.opensymphony.xwork2.util.ValueStack;

public class ExceptionAction extends FrameworkAction{

	@Autowired
	@Qualifier("isLog")
	Boolean isLog;
	
	@Autowired
	@Qualifier("isDebug")
	Boolean isDebug;
	
	private String errorMsg;
	private String debugMessage;


	@Override
	public String execute() throws Exception {
		ValueStack  s  =  (ValueStack) ActionContext.getContext().getValueStack();
		 ExceptionHolder  e; 
	        for(int  i  =  s.size();i>0;i--){ 
	                Object  obj  =  s.pop(); 
	                if(obj  instanceof  ExceptionHolder){ 
	                        e  =  (ExceptionHolder)obj; 
	                        errorMsg  =e.getException().getMessage();
	                        debugMessage  =e.getExceptionStack(); 
	                        break; 
	                }
	        } 
	        
	     if(isLog){
		     logger.error(errorMsg);
		     logger.error(debugMessage);
	     }
	     
	     if(isDebug){
	    	 getRequest().setAttribute("errorMsg", errorMsg);
	    	 getRequest().setAttribute("debugMessage", debugMessage);
	     }   
		return SUCCESS;
	}

	
}
